package com.todo.event.activity.application.util;

import com.alibaba.fastjson.JSONObject;
import com.todo.event.activity.application.entity.bo.EventDetailBO;

import java.util.List;

/**
 * @author azhebuxing
 * @date 2025/1/10 12:23
 * @description
 */
public class EventActivityUtil {


    public static final String EVENT_FIELD_USER_ID_KEY = "userId";

    /**
     * 从决策执行数据中获取用户Id
     *
     * @param payload
     * @return
     */
    public static Long getUserIdByEventDecisionPayload(JSONObject payload) {
        if (payload == null) {
            return null;
        }
        return payload.getLong(EVENT_FIELD_USER_ID_KEY);
    }

    /**
     * 从决策执行数据中获取唯一键
     *
     * @param eventDetailBO
     * @param payload
     * @return
     */
    public static String getUKByEventDecisionPayload(EventDetailBO eventDetailBO, JSONObject payload) {
        if (payload == null || eventDetailBO == null || eventDetailBO.getEventFieldList() == null) {
            return null;
        }
        List<EventDetailBO.EventField> eventFieldList = eventDetailBO.getEventFieldList();
        for (EventDetailBO.EventField eventField : eventFieldList) {
            if (eventField.getUniqueField()) {
                return payload.getString(eventField.getFieldName());
            }
        }
        return null;
    }
}
